#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "botserver.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //*****************************
    QString content;
    QString fileName = ":/mappingscript/index.html";
    QFile file(fileName);

    if( file.open(QFile::ReadOnly) ){
        content = QString(file.readAll());
    }else{
        QMessageBox::information(this, tr("GPS Mapper"),
                                      tr("Cannot load %1.").arg(fileName));
        qFatal("Error opening index html file.");
    }
    file.close();

    QWebView *frame;

    frame = ui->wvGeoMap;
    frame->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
    frame->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
    frame->setHtml(content);
    geoObject = new GeoScript(this);
    frame->page()->mainFrame()->addToJavaScriptWindowObject("geoObject",geoObject);

    //*****************************
    ui->menuView->addAction(ui->dwMap->toggleViewAction());
    ui->menuView->addAction(ui->dwController->toggleViewAction());
    ui->menuView->addAction(ui->dwSensorStream->toggleViewAction());
    ui->menuView->addAction(ui->dwVideoStream->toggleViewAction());

    latestMsg = new QString();
    setCorner( Qt::TopLeftCorner, Qt::LeftDockWidgetArea );
    setCorner( Qt::TopRightCorner, Qt::RightDockWidgetArea );
    setCorner( Qt::BottomLeftCorner, Qt::LeftDockWidgetArea );
    setCorner( Qt::BottomRightCorner, Qt::RightDockWidgetArea );

    //*****************************
    addVideo();
    cvCamera = new cv::VideoCapture();
    cvCamera->open(0);
    vidThread = new VideoThread(*cvCamera);
    connect(vidThread, SIGNAL(ImageUpdate(QImage)), this, SLOT(UpdateVideo(QImage)));
    vidThread->start();

    //*****************************
    controller = new QQuickView();
    controller->setResizeMode(QQuickView::SizeRootObjectToView);
    controller->setSource(QUrl::fromLocalFile("DPad.qml"));

    QWidget *container = QWidget::createWindowContainer(controller);
    container->setMinimumSize(controller->size());
    container->setFocusPolicy(Qt::TabFocus);
    container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    qDebug() << "Are updates Enabled: " << container->updatesEnabled();

    ui->horizontalLayout_2->addWidget(container, 0, Qt::AlignLeft);
    dPad = controller->rootObject();
    //controller = ui->decViewController->rootObject();
    connect(dPad, SIGNAL(dPadClicked(QString)), this, SLOT(controlHandler(QString)));
   // ui->decViewController->update();
}

MainWindow::~MainWindow()
{
    delete latestMsg;
    delete ui;
}

void MainWindow::geoscriptClick(void){
    //ui->lat->setText(QString::number(geoObject->getLatLng().rx()));
    //ui->lng->setText(QString::number(geoObject->getLatLng().ry()));
}

void MainWindow::addVideo()
{
    newVideo = new QLabel(this);
    newVideo->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    //newVideo->setPixmap(QPixmap::fromImage(newImage));
    ui->frmVideos->layout()->addWidget(newVideo);
    newVideo->setScaledContents(true);
}
void MainWindow::UpdateVideo(QImage newImage)
{
    //Foreach in Video list
   // qDebug() << "Image: " << newImage.width() << "," << newImage.height();
    //qDebug() << "Frame: " << ui->frmVideos->width() << "," << ui->frmVideos->height();
    //qDebug() << "New Image: " << QPixmap::fromImage(newImage).scaled(ui->frmVideos->width(),ui->frmVideos->height()).width() << "," << QPixmap::fromImage(newImage).scaled(ui->frmVideos->width(),ui->frmVideos->height()).height();
    newVideo->setPixmap(QPixmap::fromImage(newImage));
    //ui->horizontalLayout_Videos->addWidget(newVideo);

}
void MainWindow::setNewMsg(QString newMsg)
{
    *latestMsg = newMsg;
    switch(newMsg.at(0).toLatin1())
    {
//        case EnMsg_CONFIG:  configuration = new ConfigParser();
//                            configuration->msgParse(newMsg);
//                            AddBot(configuration);
//                            qDebug() << "Received a Configuration Message";
//                            break;
        case EnMsg_SENSOR:  sensorData = new SensorParser();
                            sensorData->msgParse(newMsg);
                            UpdateSensors(sensorData);
                            qDebug() << "Received a Sensor Update Message";
                            break;
        case EnMsg_ACK:     acknowledgment = new AckParser();
                            acknowledgment->msgParse(newMsg);
                            qDebug() << "Received a Acknowledgement Message";
                            break;
        default:
            *latestMsg = "Error!!!";
    }
}

void MainWindow::UpdateSensors(SensorParser* sensorData)
{

}

void MainWindow::controlHandler(QString direct)
{
    if(direct == CTRL_UP)
        qDebug()<<"Bot Moving Up";
    else if(direct == CTRL_DOWN)
        qDebug() << "Bot Moving Down";
    else if(direct == CTRL_LEFT)
        qDebug()<<"Bot Moving Left";
    else if(direct == CTRL_RIGHT)
        qDebug()<<"Bot Moving Right";
    else if(direct == CTRL_STOP)
        qDebug()<<"Bot has Stopped";
    else
        qDebug()<<"Undefined Direction";

}
